package com.siwei.apply.service.impl;

import com.siwei.apply.domain.Project;
import com.siwei.apply.domain.ProjectWorkflow;
import com.siwei.apply.domain.Workflow;
import com.siwei.apply.domain.Ydysyxz;
import com.siwei.apply.domain.res.YdysyxzRes;
import com.siwei.apply.domain.vo.YdysyxzUpdateVo;
import com.siwei.apply.domain.vo.YdysyxzVo;
import com.siwei.apply.mapper.ProjectMapper;
import com.siwei.apply.mapper.ProjectWorkflowMapper;
import com.siwei.apply.mapper.WorkflowMapper;
import com.siwei.apply.mapper.YdysyxzMapper;
import com.siwei.apply.service.YdysyxzService;
import com.siwei.common.core.utils.bean.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import static com.siwei.apply.common.Common.UserId;

/**
 * 用地预审与选址 服务实现类
 */
@Service
public class YdysyxzImpl implements YdysyxzService {
    @Autowired
    private YdysyxzMapper ydysyxzMapper;
    @Autowired
    private ProjectMapper projectMapper;
    @Autowired
    private ProjectWorkflowMapper projectWorkflowMapper;
    @Autowired
    private WorkflowMapper workflowMapper;

    @Override
    public Boolean isExit(String projectId) {
        return ydysyxzMapper.isExit(projectId);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public String add(YdysyxzVo ydysyxzVo) {
        // 添加
        Ydysyxz ydysyxz = new Ydysyxz();
        BeanUtils.copyProperties(ydysyxzVo, ydysyxz);
        ydysyxz.generateId();
        ydysyxz.setCreatorId(UserId);
        ydysyxzMapper.add(ydysyxz);
        // 添加项目流程
        Integer projectType = projectMapper.getProjectTypeById(ydysyxz.getProjectId());
        Workflow workflow = workflowMapper.selectByProjectTypeAndTableName(projectType, "t_ydysyxz");
        ProjectWorkflow projectWorkflow = new ProjectWorkflow();
        projectWorkflow.setId(ydysyxz.getId());
        projectWorkflow.setParentId("");
        projectWorkflow.setWorkflowId(workflow.getId());
        projectWorkflow.setProjectId(ydysyxz.getProjectId());
        projectWorkflow.setNodeId(ydysyxz.getId());
        projectWorkflow.setNodeTableName(workflow.getTableName());
        projectWorkflow.setWorkflowName(workflow.getName());
        projectWorkflowMapper.add(projectWorkflow);
        return ydysyxz.getId();
    }

    @Override
    public YdysyxzRes get(String projectId) {
        Ydysyxz ydysyxz = ydysyxzMapper.get(projectId);
        Project project = projectMapper.get(projectId);

        YdysyxzRes ydysyxzRes = new YdysyxzRes();
        if (ydysyxz != null) {
            BeanUtils.copyProperties(ydysyxz, ydysyxzRes);
        }
        ydysyxzRes.setProjectId(projectId);
        ydysyxzRes.setProjectName(project.getName());
        ydysyxzRes.setProjectCode(project.getCode());
        ydysyxzRes.setProjectCompany(project.getCompany());
        return ydysyxzRes;
    }

    @Override
    public YdysyxzRes getById(String id) {
        Ydysyxz ydysyxz = ydysyxzMapper.getById(id);
        if (ydysyxz == null) {
            return null;
        }
        Project project = projectMapper.get(ydysyxz.getProjectId());
        YdysyxzRes ydysyxzRes = new YdysyxzRes();
        BeanUtils.copyProperties(ydysyxz, ydysyxzRes);
        ydysyxzRes.setProjectId(ydysyxz.getProjectId());
        ydysyxzRes.setProjectName(project.getName());
        ydysyxzRes.setProjectCode(project.getCode());
        ydysyxzRes.setProjectCompany(project.getCompany());
        return ydysyxzRes;
    }

    @Override
    public void update(YdysyxzUpdateVo ydysyxzUpdateVo) {
        ydysyxzMapper.update(ydysyxzUpdateVo);
    }

    @Override
    public void updateHasOnchain(String id, Boolean hasOnchain) {
        ydysyxzMapper.updateHasOnchain(id, hasOnchain);
    }
}
